<template>
    <a-tree
            checkable
            :defaultExpandParent="true"
            :defaultExpandAll="true"
            :checkStrictly="true"
            :autoExpandParent="true"
            v-model="checkedKeys"
            :treeData="treeData"
    />

</template>

<script>
    import {Tree,message} from 'ant-design-vue'
    export default {
        components:{
          ATree:Tree
        },

        props:['value'],
        name: "RolePermissionEdit"
        ,
        data(){

            return {
                checkedKeys:[],
                treeData:[]

            };
        },
        mounted(){

            this.loadTreeData();
        },
        methods:{

            submitData(){


                this.$axios.put("/auth/role/updatePermission?roleId="+this.value,this.checkedKeys.checked)
                    .then(d=>{
                        let result = d.data;

                        if(result.status == 0){
                            message.success("权限信息更新成功");
                            this.$emit("ok");
                        }else{
                            message.error(result.message);
                        }

                    })
                    .catch(e=>{
                        message.error("权限信息保存失败");
                    })

            },

            loadTreeData(){

                if(this.value  && this.value != ''){

                    this.$axios.get("/auth/permission/getPermissionTree")
                        .then(d=>{
                            if(d.data.status == 0){
                                this.treeData = this.createTreeNode( d.data.data);

                                this.loadRoleData();
                            }
                        })
                        .catch(e=>{

                        })
                    }
            },

            loadRoleData(){

                if(this.value  && this.value != ''){

                    this.$axios.get("/auth/role/getRolePermissionList?roleId=" + this.value)
                        .then(d=>{
                            if(d.data.status == 0){

                                let plist = d.data.data;

                                this.checkedKeys = [];
                                for(let p of plist){
                                    this.checkedKeys.push(p.permissionId);
                                }
                            }
                        })
                        .catch(e=>{

                            message.error("权限信息加载失败");
                        })
                }
            },

            createTreeNode(nodes){

                let treeNodes = [];

                for(let node of nodes){

                    let tnode = {
                        key:node.permission.permissionId,
                        title:node.permission.permissionName,
                    };

                    if(node.children.length > 0){
                        tnode.children = this.createTreeNode(node.children);
                    }

                    treeNodes.push(tnode);
                }

                return treeNodes;
            }
        },
        watch:{
            value(nv){
                this.loadRoleData();
            }
        }
    }
</script>

<style scoped>

</style>